package com.pug.zixun.controller.login;

import com.pug.zixun.bo.UserBo;
import com.pug.zixun.commons.enums.AdminUserResultEnum;
import com.pug.zixun.commons.utils.fn.asserts.Vsserts;
import com.pug.zixun.commons.utils.pwd.MD5Util;
import com.pug.zixun.config.redis.AdminRedisKeyManager;
import com.pug.zixun.config.redis.IJwtBlackService;
import com.pug.zixun.controller.BaseController;
import com.pug.zixun.config.jwt.JwtService;
import com.pug.zixun.domain.User;
import com.pug.zixun.service.user.IUserService;
import com.pug.zixun.vo.UserVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.UUID;

/**
 * @Description:
 * @Author: WengYu
 * @CreateTime: 2022/06/06 21:52
 */
@RestController
@CrossOrigin
@Slf4j
public class PassportLoginController extends BaseController implements AdminRedisKeyManager {
    @Autowired
    private IUserService userService;
    @Autowired
    private JwtService jwtService;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private IJwtBlackService jwtBlackService;

    @PostMapping("/login/toLogin")
    public UserBo login(@RequestBody UserVo userVo){
        log.info("uservo----->{}",userVo);
        // 对用户名和密码非空校验
        Vsserts.isEmptyEx(userVo.getUsername(), AdminUserResultEnum.USER_NAME_NOT_EMPTY);
        Vsserts.isEmptyEx(userVo.getPassword(), AdminUserResultEnum.USER_PWD_NOT_EMPTY);
        // 查询用户是否存在
        User dbLoginUser = userService.login(userVo);
        Vsserts.isNullEx(dbLoginUser,AdminUserResultEnum.USER_NULL_ERROR);
        // 判断密码是否正确
        boolean isLoin = dbLoginUser.getPassword().equals(MD5Util.md5slat(userVo.getPassword()));
        Vsserts.isFalseEx(isLoin,AdminUserResultEnum.USER_INPUT_USERNAME_ERROR);
        // 构建UserBo返回前端
        UserBo userBo = new UserBo();
        String token = jwtService.createToken(dbLoginUser.getId());
        userBo.setToken(token);
        dbLoginUser.setPassword(null);
        userBo.setUser(dbLoginUser);

        
        String tokenUuid = UUID.randomUUID().toString();
        String tokenUuidKey = USER_LOGIN_LOGOUT_KEY + dbLoginUser.getId();
        stringRedisTemplate.opsForValue().set(tokenUuidKey,tokenUuid);
        userBo.setTokenUuid(tokenUuid);

        // 闯将双倍时间 勇于续期
        jwtService.redisToken(token);

        return userBo;
    }


    @PostMapping("/login/logout")
    public String logout(HttpServletRequest request){
        // 通过请求头获取
        String token = jwtService.getToken(request);
        String userId = jwtService.getTokenUserId(request);

        Vsserts.isEmptyEx(token,AdminUserResultEnum.TOKEN_NOT_FOUND);
        Vsserts.isNullEx(userId,AdminUserResultEnum.USER_NAME_NOT_EMPTY);

        // 删除下线的uuid
        String tokenUuidKey = USER_LOGIN_LOGOUT_KEY + userId;
        stringRedisTemplate.delete(tokenUuidKey);

        // 删除续期redis的key
        String tokenKey = USER_LOGIN_TOKEN_KEY + token;
        stringRedisTemplate.delete(tokenKey);
        // 加黑名单
        jwtBlackService.addBlackList(token);

        return "logout success";
    }
}
